package com.hxzy.springbootes.mybatis.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.springbootes.es.entity.EsHotel;
import com.hxzy.springbootes.es.service.EsHotalService;
import com.hxzy.springbootes.mybatis.entity.TbHotel;
import com.hxzy.springbootes.mybatis.service.TbHotelService;
import com.hxzy.springbootes.mybatis.mapper.TbHotelMapper;
import com.hxzy.springbootes.req.HotelAddReq;
import com.hxzy.springbootes.req.HotelEditReq;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 11452
* @description 针对表【tb_hotel】的数据库操作Service实现
* @createDate 2023-05-31 15:42:17
*/
@Service
public class TbHotelServiceImpl extends ServiceImpl<TbHotelMapper, TbHotel>
    implements TbHotelService{


    @Autowired
    private EsHotalService esHotalService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insert(HotelAddReq req) {
        //对象copy   没有使用mapstruct
        TbHotel  entity= new TbHotel();
        BeanUtils.copyProperties(req,entity);
        //向数据库mysql中新增中
        int count = this.getBaseMapper().insert(entity);
        if(count>0){
            //同步数据到ES中 把db转换为es的模型
            EsHotel esHotel=new EsHotel(entity);
            //3、把esHotal插入到es索引
            this.esHotalService.save(esHotel);
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean edit(HotelEditReq req) {
        TbHotel db = this.getBaseMapper().selectById(req.getId());
        if(db==null){
            throw new RuntimeException("数据库不存在");
        }

        //把值转给数据库
        BeanUtils.copyProperties(req,db);
        int count = this.getBaseMapper().updateById(db);
        if(count>0){
            //同步数据到ES中 把db转换为es的模型
            EsHotel esHotel=new EsHotel(db);
            //3、把esHotal插入到es索引
            this.esHotalService.save(esHotel);
            return true;
        }
        return false;
    }
}




